package com.djdevs.calculatorconverterfree;

import android.app.Activity;
import android.content.Intent;
import android.content.res.*;
import android.os.Bundle;
import android.text.*;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.*;
import java.util.*;

public class ConvActivity extends Activity {

	ReadXml rxConverter = new ReadXml();
	ConverterParser cnvParser = new ConverterParser();
	Converter category = new Converter();
	ConvUnit cnvFrom = new ConvUnit();
	ConvUnit cnvTo =  new ConvUnit();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_convertor);
        try
        {
	        SetSpnCategoryOnSelectListener();
	        SetConverterData();
	        SetCategoryList();
	        SetConvButtonOnClickListener();
	        SetSpnCategoryOnSelectListener();
	        SetSpnUnitFromOnSelectListener();
	        SetSpnUnitToOnSelectListener();
	        SetFromTextAfterChangedListener();
        }
        catch (Exception e)
        {
        	
        }
    }

    public void SetConvButtonOnClickListener() {
        final Button btn = (Button) findViewById(R.id.btnCalc);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
            	try
            	{
	                Intent intent = new Intent(ConvActivity.this, FullscreenActivity.class);
	                startActivity(intent);
            	}
            	catch (Exception e)
            	{
            		
            	}
            }
        });
    }

    public void SetConverterData()
    {
    	try
    	{
    		XmlResourceParser xrpConverter = getResources().getXml(R.xml.converter);
    		rxConverter = new ReadXml(xrpConverter);
    	}
  		catch (Exception e) {
			e.printStackTrace();
		} 
    }
    
    public void SetCategoryList()
    {
    	try
    	{
	    	Spinner spnCategory = (Spinner)findViewById(R.id.spnCategory);
	    	ArrayList<String> lstCategory = rxConverter.GetConverterList();
	    	ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lstCategory);
			dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spnCategory.setAdapter(dataAdapter);
    	}
    	catch (Exception e)
    	{
    		
    	}
    }
    
    public void SetSpnCategoryOnSelectListener()
    {
    	final Spinner spnCategory = (Spinner)findViewById(R.id.spnCategory);
    	spnCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
    		public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    			try
    	    	{
    				EditText etFrom = (EditText)findViewById(R.id.btnEditFromValue);
    				TextView tTo = (TextView)findViewById(R.id.toValue);
    				etFrom.setText("0");
    				tTo.setText("0");
	    			Toast.makeText(parent.getContext(), "Category: " + parent.getItemAtPosition(pos).toString()
	    					, Toast.LENGTH_SHORT).show();
	    			category = rxConverter.GetConverterForCategory(parent.getItemAtPosition(pos).toString());
	    			ConvUnit baseUnit = rxConverter.GetBaseConverterUnit(category.type);
	    			cnvParser.SetBaseUnit(baseUnit.unitName, baseUnit.unitCode, 0.0
	    					, baseUnit.conversionFactor, baseUnit.defaultZero, category.type);
	    			
	    			SetUnitFromList(parent.getItemAtPosition(pos).toString());
	    			SetUnitToList(parent.getItemAtPosition(pos).toString());
    	    	}
    			catch (Exception e)
    			{
    				
    			}
    		}

    		public void onNothingSelected(AdapterView<?> arg0) { 
    			// Do Noting
    		}
    	});
    }
        
    public void SetUnitFromList(String categoryType)
    {
    	try
    	{
	    	Spinner spnUnitFrom = (Spinner)findViewById(R.id.spnUnitFrom);
	    	ArrayList<String> lstUnitFrom = rxConverter.GetUnitListForConverter(categoryType);
	    	ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lstUnitFrom);
			dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spnUnitFrom.setAdapter(dataAdapter);
			spnUnitFrom.setSelection(dataAdapter.getPosition(rxConverter.GetBaseConverterUnitString(categoryType)));
			
    	}
    	catch (Exception e)
    	{
    		
    	}
    }
    
    public void SetUnitToList(String categoryType)
    {
    	try
    	{
	    	Spinner spnUnitTo = (Spinner)findViewById(R.id.spnUnitTo);
	    	ArrayList<String> lstUnitTo = rxConverter.GetUnitListForConverter(categoryType);
	    	ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lstUnitTo);
			dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spnUnitTo.setAdapter(dataAdapter);
			spnUnitTo.setSelection(dataAdapter.getPosition(rxConverter.GetBaseConverterUnitString(categoryType)));
    	}
    	catch (Exception e)
    	{
    		
    	}
    }
    
    public void SetSpnUnitFromOnSelectListener()
    {
    	final Spinner spnUnitFrom = (Spinner)findViewById(R.id.spnUnitFrom);
    	spnUnitFrom.setOnItemSelectedListener(new OnItemSelectedListener() {
    		public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    			try
    			{
	    			Toast.makeText(parent.getContext(), "Unit: " + parent.getItemAtPosition(pos).toString()
	    					, Toast.LENGTH_SHORT).show();
	    			final Spinner spnUnitTo = (Spinner)findViewById(R.id.spnUnitTo);
	    			String unitFrom = parent.getItemAtPosition(pos).toString();
	    			String unitTo = spnUnitTo.getSelectedItem().toString();
	    			SetConverterParserFromTo(unitFrom, unitTo);
	    			cnvParser.ConvertUnit();
	    			TextView tTo = (TextView)findViewById(R.id.toValue);
	    			tTo.setText(ConverterParser.FormatNumber(cnvParser.getSecondUnitValue()));
    			}
    			catch (Exception e)
    			{
    				
    			}
    		}

    		public void onNothingSelected(AdapterView<?> arg0) { 
    			// Do Noting
    		}
    	});
    }
    
    public void SetSpnUnitToOnSelectListener()
    {
    	final Spinner spnUnitTo = (Spinner)findViewById(R.id.spnUnitTo);
    	spnUnitTo.setOnItemSelectedListener(new OnItemSelectedListener() {
    		public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    			try
    			{
	    			Toast.makeText(parent.getContext(), "Unit: " + parent.getItemAtPosition(pos).toString()
	    					, Toast.LENGTH_SHORT).show();
	    			final Spinner spnUnitFrom = (Spinner)findViewById(R.id.spnUnitFrom);
	    			String unitTo = parent.getItemAtPosition(pos).toString();
	    			String unitFrom = spnUnitFrom.getSelectedItem().toString();
	    			SetConverterParserFromTo(unitFrom, unitTo);
	    			cnvParser.ConvertUnit();
	    			TextView tTo = (TextView)findViewById(R.id.toValue);
	    			tTo.setText(ConverterParser.FormatNumber(cnvParser.getSecondUnitValue()));
    			}
    			catch (Exception e)
    			{
    				
    			}
    		}

    		public void onNothingSelected(AdapterView<?> arg0) { 
    			// Do Noting
    		}
    	});
    }
    
    public void SetConverterParserFromTo(String unitFrom, String unitTo)
    {
    	try
    	{
			cnvFrom = rxConverter.GetConverterUnitForCategoryType(category, unitFrom);
			EditText etFrom = (EditText)findViewById(R.id.btnEditFromValue);
			cnvParser.SetFirstUnit(cnvFrom.unitName, cnvFrom.unitCode, Double.parseDouble(etFrom.getText().toString())
					, cnvFrom.conversionFactor, cnvFrom.defaultZero);
			
			cnvTo = rxConverter.GetConverterUnitForCategoryType(category, unitTo);
			TextView tTo = (TextView)findViewById(R.id.toValue);
			cnvParser.SetSecondUnit(cnvTo.unitName, cnvTo.unitCode, 0.0, cnvTo.conversionFactor, cnvTo.defaultZero);
    	}
    	catch (Exception e)
    	{
    		
    	}
    }
    
    public void SetFromTextAfterChangedListener()
    {
    	EditText etFrom = (EditText)findViewById(R.id.btnEditFromValue);
    	etFrom.addTextChangedListener(new TextValidator(etFrom) {
    	    @Override
    	    public void validate(EditText textView, String text) {
    	    	try
    	    	{
    	    		EditText etFrom = (EditText)findViewById(R.id.btnEditFromValue);
    	    		String fromText = etFrom.getText().toString();
    	    		if (fromText.equalsIgnoreCase("")) {
    	    			TextView tTo1 = (TextView)findViewById(R.id.toValue);
    	    			tTo1.setText("");
					}
    	    		else
    	    		{
    	    			final Spinner spnUnitFrom = (Spinner)findViewById(R.id.spnUnitFrom);
    	    	    	final Spinner spnUnitTo = (Spinner)findViewById(R.id.spnUnitTo);
    	    	    	
    	    	    	String unitFrom = spnUnitFrom.getSelectedItem().toString();
    	    			String unitTo = spnUnitTo.getSelectedItem().toString();
    	    			SetConverterParserFromTo(unitFrom, unitTo);
    	    			cnvParser.ConvertUnit();
    	    			TextView tTo = (TextView)findViewById(R.id.toValue);
    	    			tTo.setText(ConverterParser.FormatNumber(cnvParser.getSecondUnitValue()));
    	    		}
    	    	}
    	    	catch (Exception e)
    	    	{
    	    		
    	    	}
    	    }
    	});
    }
}


abstract class TextValidator implements TextWatcher {
    private final EditText etText;

    public TextValidator(EditText textView) {
        this.etText = textView;
    }

    public abstract void validate(EditText textView, String text);

    @Override
    final public void afterTextChanged(Editable s) {
    	try
    	{
	        String text = etText.getText().toString();
	        validate(etText, text);
    	}
    	catch (Exception e)
    	{
    		
    	}
    }

    @Override
    final public void beforeTextChanged(CharSequence s, int start, int count, int after) { /* Don't care */ }

    @Override
    final public void onTextChanged(CharSequence s, int start, int before, int count) { /* Don't care */ }
}